﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOMSWinService.DATA;
using BOMSWinService.Control;
using System.Diagnostics;


namespace BOMSWinService
{
    public partial class SettingCfg : Form
    {
        int Distance = 30;
        public SettingCfg()
        {
            InitializeComponent();
            LoadIPEntity();
        }
        private void LoadIPEntity()
        {
            DataUti du = new DataUti();
            List<EquipModel> IpList = null;
            try
            {
                IpList = du.GetEquipModelList();
            }
            catch (Exception ex)
            {
                Log("无法链接到数据库。"+ex.Message);
            }
            if (IpList != null)
            {
                for (int i = 0; i < IpList.Count; i++)
                {
                    UCInputIP Uip = new UCInputIP();
                    Uip.IP = IpList[i].ClientHostIP;
                    Uip.Port = IpList[i].Port;

                    panel1.Controls.Add(Uip);
                    Uip.Top = 5 + i * Distance;
                    Uip.Left = 5;
                }
            }
        }
        private void Log(string content)
        {
            eventLog1.WriteEntry(content, EventLogEntryType.Information);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UCInputIP Uip = new UCInputIP();
            Uip.Top = GetNewItemTop();
            Uip.Left = 5;
            panel1.Controls.Add(Uip);           

        }
        private int GetNewItemTop()
        {
            int L= panel1.Controls.Count;
            int n = 0;
            for (int i = 0; i < L; i++)
            {
                if (panel1.Controls[i].Visible)
                {
                    n += 1;
                }
            }
            return 5 + n * Distance;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int L = panel1.Controls.Count;
            
            for (int i = L-1; i >=0; i--)
            {
                UCInputIP uip=(UCInputIP)panel1.Controls[i];
                if (uip.IsChecked)
                {                    
                    uip.Visible = false;
                    uip.Dispose();
                }
            }
            RebuildPage();
        }
        private void RebuildPage()
        {
            int L = panel1.Controls.Count;
            int m = 0;
            for (int i =0; i<L; i++)
            {
                UCInputIP uip = panel1.Controls[i] as UCInputIP;
                if (uip != null && uip.Visible)
                {                   
                    uip.Top = 5 + m++ * Distance;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<EquipModel> List = new List<EquipModel>();
            EquipModel model = null;
            int L = panel1.Controls.Count;
            
            for (int i = 0; i < L; i++)
            {
                UCInputIP uip = panel1.Controls[i] as UCInputIP;
                if (uip != null && uip.Visible)
                {
                    model = new EquipModel();
                    model.ClientHostIP = uip.IP;
                    model.Port = uip.Port;
                    List.Add(model);
                }
            }
            DataUti data = new DataUti();
            data.SaveClientIP(List);
            MessageBox.Show("保存成功！");
        }
    }
}
